import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


# 1. 创建Series
s1 = pd.Series([10,20,30,40], index=["a","b","c","d"])
print("=== Series ===")
print(s1, "\n索引：", s1.index, " 值：", s1.values)
# 2. 创建DataFrame
data = {
    "姓名": ["张三","李四","王五","赵六","钱七"],
    "年龄": [18,19,18,20,19],
    "语文成绩": [85,92,78,90,88],
    "数学成绩": [90,85,82,95,86]
}
df = pd.DataFrame(data)

# 数据准备
students = df["姓名"]
math_scores = df["数学成绩"]

# 中文字体设置（避免乱码）
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

x = np.arange(len(students))  # x轴位置
width = 0.35  # 柱子宽度
plt.figure(figsize=(10,6))
# 并列柱状图
plt.bar(x - width/2, df["语文成绩"], width, label='语文', color='#ff7f0e')
plt.bar(x + width/2, df["数学成绩"], width, label='数学', color='#2ca02c')
plt.xticks(x, students)  # 替换x轴标签为姓名
plt.title('语文与数学成绩对比', fontsize=14)
plt.xlabel('学生姓名')
plt.ylabel('成绩（分）')
plt.legend()
plt.grid(True, alpha=0.3, axis='y')  # 仅y轴网格
plt.show()